package com.yourenbang.lingyun.transaction.web.service.impl.pay;

import com.yourenbang.lingyun.constants.BizCodeMessages;
import com.yourenbang.lingyun.core.exception.BizRuntimeException;
import com.yourenbang.lingyun.core.util.BizAssertUtils;
import com.yourenbang.lingyun.core.util.KLog;
import com.yourenbang.lingyun.domain.PayTransactionIndex;
import com.yourenbang.lingyun.domain.PayTransactionIndexExample;
import com.yourenbang.lingyun.mapper.PayTransactionIndexMapper;
import com.yourenbang.lingyun.transaction.mapper.TransactionPayTransactionIndexMapper;
import com.yourenbang.lingyun.transaction.mapper.TransactionPayTransactionMapper;
import com.yourenbang.lingyun.transaction.web.service.impl.pay.klks.KlksPayConfig;
import com.yourenbang.lingyun.transaction.web.service.impl.pay.transaction.PayTransactionConfig;
import com.yourenbang.lingyun.transaction.web.service.pay.PayTransactionOrderService;
import com.yourenbang.transaction.api.model.vo.pay.PayTransaction;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service("payTransactionOrderService")
@Slf4j
public class PayTransactionOrderServiceImpl implements PayTransactionOrderService {

    @Autowired
    private TransactionPayTransactionMapper payTransactionMapper;
    @Autowired
    private TransactionPayTransactionIndexMapper tsPayTransactionIndexMapper;
    @Autowired
    private PayTransactionIndexMapper payTransactionIndexMapper;

    /**
     * 通过订单号号获取订单信息(不推荐)
     */
    @Override
    public PayTransaction getByOrderNo(String orderNo) {
        PayTransactionIndex index = tsPayTransactionIndexMapper.getByOrderNo(orderNo);
        if (index == null) {
            return null;
        }
        return payTransactionMapper.getByOrderNo(index.getTableIndex(), orderNo);
    }

    /**
     * 通过订单号号获取订单信息(推荐)
     */
    @Override
    public PayTransaction getByOrderNo(String userBaseId, String orderNo) {
        return payTransactionMapper.getByOrderNo(KlksPayConfig.getTransTableIndex(userBaseId), orderNo);
    }

    /**
     * 通过系统参考号获取订单信息
     */
    @Override
    public PayTransaction getBySysRef(String userBaseId, String sysRef, String merchantNo, Short channelId, Short platNo) {
        return payTransactionMapper.getBySysRef(KlksPayConfig.getTransTableIndex(userBaseId), sysRef, merchantNo, channelId, platNo);
    }

    /**
     * 插入
     */
    @Override
    public void insert(PayTransaction po) {
        if (StringUtils.isBlank(po.getUserBaseId())) {
            throw new BizRuntimeException(BizCodeMessages.VALIDATOR_ERR, "保存订单,userBaseId空");
        }

        int index = KlksPayConfig.getTransTableIndex(po.getUserBaseId());

        int inTrans = payTransactionMapper.insert(index, po);
        BizAssertUtils.assertOne(inTrans, "保存订单失败");

        PayTransactionIndex inIndex = new PayTransactionIndex();
        inIndex.setOrderNo(po.getOrderNo());
        inIndex.setTransactionNo(po.getTransactionNo());
        inIndex.setTableIndex(index);
        inIndex.setUserBaseId(po.getUserBaseId());
        inIndex.setMerchantId(po.getMerchantId());
        inIndex.setMerchantNo(po.getMerchantNo());
        inIndex.setCardFlag(po.getCardFlag());
        inIndex.setTransactionAmount(po.getTransactionAmount());
        inIndex.setTransactionTime(po.getTransactionTime());
        inIndex.setTransactionMonth(po.getTransactionMonth());
        inIndex.setTransactionDay(po.getTransactionDay());
        inIndex.setStatus(po.getStatus());
        inIndex.setCreateTime(po.getCreateTime());
        inIndex.setChannelId(po.getChannelId());
        inIndex.setPlatNo(po.getPlatNo());
        inIndex.setClientNo(po.getClientNo());
        int inIndexCc = payTransactionIndexMapper.insertSelective(inIndex);
        BizAssertUtils.assertOne(inIndexCc, "保存订单索引失败");
    }

    /**
     * 更新
     */
    @Override
    public void updateById(PayTransaction po) {
        if (po.getPayTransactionId() == null) {
            throw new BizRuntimeException(BizCodeMessages.VALIDATOR_ERR, "更新订单,Id空");
        }
        if (StringUtils.isBlank(po.getUserBaseId())) {
            throw new BizRuntimeException(BizCodeMessages.VALIDATOR_ERR, "更新订单,UserBaseId空");
        }
        if (StringUtils.isBlank(po.getOrderNo())) {
            throw new BizRuntimeException(BizCodeMessages.VALIDATOR_ERR, "更新订单,OrderNo空");
        }

        int upTrans = payTransactionMapper.updateByIdWithVersion(KlksPayConfig.getTransTableIndex(po.getUserBaseId()), po);
        BizAssertUtils.assertOne(upTrans, "更新订单失败");

        updateTranIndexByOrderNo(po);
    }

    /**
     * 根据id更新(不判断乐观锁)
     */
    @Override
    public void updateByIdNoVersion(PayTransaction po) {
        if (po.getPayTransactionId() == null) {
            throw new BizRuntimeException(BizCodeMessages.VALIDATOR_ERR, "更新订单,Id空");
        }
        if (StringUtils.isBlank(po.getUserBaseId())) {
            throw new BizRuntimeException(BizCodeMessages.VALIDATOR_ERR, "更新订单,UserBaseId空");
        }
        if (StringUtils.isBlank(po.getOrderNo())) {
            throw new BizRuntimeException(BizCodeMessages.VALIDATOR_ERR, "更新订单,OrderNo空");
        }

        int upTrans = payTransactionMapper.updateByIdNoVersion(KlksPayConfig.getTransTableIndex(po.getUserBaseId()), po);
        BizAssertUtils.assertOne(upTrans, "更新订单失败");

        updateTranIndexByOrderNo(po);
    }

    @Override
    public PayTransactionIndex getPayTransactionIndexByPayTransaction(PayTransaction po) {
        PayTransactionIndex payTransactionIndex = new PayTransactionIndex();
        KLog.info("根据订单获得订单索引记录入参:{}",po.toJSON());
        BeanUtils.copyProperties(po, payTransactionIndex);
        payTransactionIndex.setTableIndex(PayTransactionConfig.getTransTableIndex(po.getUserBaseId()));
        KLog.info("根据订单获得订单索引记录出参:{}",payTransactionIndex.toString());
        return payTransactionIndex;
    }

    /**
     * 更新索引表
     */
    private void updateTranIndexByOrderNo(PayTransaction po) {

        PayTransactionIndex upIndex = new PayTransactionIndex();
        upIndex.setOrderNo(po.getOrderNo());
        upIndex.setTransactionNo(po.getTransactionNo());
        upIndex.setMerchantNo(po.getMerchantNo());
        upIndex.setCardFlag(po.getCardFlag());
        upIndex.setTransactionAmount(po.getTransactionAmount());
        upIndex.setTransactionTime(po.getTransactionTime());
        upIndex.setTransactionMonth(po.getTransactionMonth());
        upIndex.setTransactionDay(po.getTransactionDay());
        upIndex.setStatus(po.getStatus());

        PayTransactionIndexExample indexExample = new PayTransactionIndexExample();
        indexExample.createCriteria()
                .andOrderNoEqualTo(po.getOrderNo());
        int upIndexcc = payTransactionIndexMapper.updateByExampleSelective(upIndex, indexExample);
        BizAssertUtils.assertOne(upIndexcc, "更新订单索引失败");
    }

    /**
     * 用户是否有电签交易
     */
    @Override
    public int countByUserEpos(String userBaseId) {
        return payTransactionMapper.countByUserEpos(KlksPayConfig.getTransTableIndex(userBaseId), userBaseId);
    }
}
